package com.css.fxfzdzzh.modules.reportManagement.controller;

import com.css.fxfzdzzh.base.response.RestResponse;
import com.css.fxfzdzzh.modules.reportManagement.entity.BreakReport;
import com.css.fxfzdzzh.modules.reportManagement.param.PageQueryParam;
import com.css.fxfzdzzh.modules.reportManagement.service.ReportService;
import com.css.fxfzdzzh.util.PlatformPageUtils;
import com.css.fxfzdzzh.util.ResponseCreator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年05月09日  15:16
 * 地表破裂带报告评估
 */
@RestController
@RequestMapping("/breakReport")
public class ReportController {

    @Resource
    private ReportService reportService;

    /**
     *  地表破裂带报告分页查询
     */
    @GetMapping("/breakPage")
    public RestResponse breakPage(PageQueryParam param, HttpServletRequest request){
        int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize=Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String,Object> page=reportService.breakReportPage(param,curPage,pageSize);
        return ResponseCreator.createSuccessResponse(page);
    }

    /**
     *  地表破裂带报告生成
     */
    @PostMapping("/createBreakReport")
    public RestResponse createBreakReport(@RequestBody  Map<String,Object> map){
        try{
            BreakReport report = reportService.createBreakReport((String) map.get("num"));
            return ResponseCreator.createSuccessResponse("报告生成成功");
        }catch (Exception e){
            return ResponseCreator.createFailResponse("报告生成失败");
        }
    }

    /**
     * 查询报告信息
     */
    @GetMapping("/getReport")
    public RestResponse getReport(@RequestParam(value = "reportId",required = false) String reportId){
        if(StringUtils.isBlank(reportId)){
            return ResponseCreator.createSuccessResponse(new BreakReport());
        }
        return ResponseCreator.createSuccessResponse(reportService.getReport(reportId));
    }

    /***
     * 报告下载
     * @return
     */
    @PostMapping("/downloadReport")
    public void downloadReport(@RequestBody Map<String,Object> map, HttpServletResponse res){
        String reportId =(String) map.get("reportId");
        String type =(String) map.get("type");
        reportService.downloadReport(reportId,type,res);
    }

    /***
     * 上传报告
     * @param file
     * @return
     */
    @PostMapping("/uploadReport")
    public RestResponse uploadReport(@RequestParam("file") MultipartFile file, @RequestParam("num") String num) throws IOException {
        return reportService.uploadReport(file,num);
    }

    @GetMapping("/getHistoryReport")
    public RestResponse getHistoryReport(@RequestParam("num") String num,@RequestParam("reportType") String reportType, HttpServletRequest request){
        int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String,Object> page=reportService.getHistoryReport(num,reportType,curPage,pageSize);
        return ResponseCreator.createSuccessResponse(page);
    }

    /***
     * 历史报告下载
     * @param map
     * @return
     */
    @PostMapping("/downloadHistoryReport")
    public void downloadHistoryReport( @RequestBody Map<String,Object> map,HttpServletResponse res) throws Exception {
        String dthrId =(String) map.get("dthrId");
        reportService.downloadHistoryReport(dthrId,res);
    }

    /***
     * 提交审核
     * @param map
     * @return
     */
    @PostMapping("/submitCheak")
    public RestResponse submitCheak( @RequestBody Map<String,Object> map) throws Exception {
        String reportId =(String) map.get("reportId");
        reportService.submitCheak(reportId);
        return ResponseCreator.createSuccessResponse("审核提交成功");
    }

    /***
     * 获取回退意见
     * @param reportId 报告id
     * @return
     */
    @GetMapping("/getBackReportMsg")
    public RestResponse getBackReport(@RequestParam("reportId") String reportId){
        return ResponseCreator.createSuccessResponse(reportService.getBackReport(reportId));
    }
}
